<?php
/** 
* publiccontroller.php
* Create on 2015-04-03 19:35:26
* Create by yuebin
*/
namespace haibao\cms\data\control;


class PublicController extends \haibao\cms\data\BaseMysql {
	const BBS_ATTACTHMENT_FRONT = 'data/attachment/forum/';
	const BBS_USER_PORTRAIT = 'http://bbs.haibao.com/uc_server/avatar.php?uid=';
	private static  $webSitePlateCatche = 'plateCatcheData';
	
	public function __construct() {
		parent::__construct ( '\haibao\cms\model\data\Base' );
	}
	
 	public function getPkThreadData($limit){
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
    	$threadProId = array();
    	$tids = array();
    	$data = array();
    	$threadData = array();
    
    	$sql="SELECT
    			byx.id,
		    	byx.thread_id_1,
		    	byx.thread_id_2,
		    	post.`subject` as leftSubject,
		    	post2.`subject` as rightSubject,
		    	post.`replies` as leftReplies,
		    	post2.`replies` as rightReplies
	    	FROM
	    		hb_zj_pk_byx AS byx
	    	LEFT JOIN hb_forum_thread AS post ON byx.thread_id_1 = post.tid
	    	LEFT JOIN hb_forum_thread AS post2 ON byx.thread_id_2 = post2.tid
	    		
	    	WHERE
	    		byx.STATUS = '1'
	    	ORDER BY
	    		byx.id DESC
	    	LIMIT $limit";
    
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data['thread_id_1']['pkId'] = $result->getData('id');
    		$data['thread_id_2']['pkId'] = $result->getData('id');
    		$data['thread_id_1']['subject'] = $result->getData('leftSubject');
    		$data['thread_id_2']['subject'] = $result->getData('rightSubject');
    		$data['thread_id_1']['id'] = $result->getData('thread_id_1');
    		$data['thread_id_2']['id'] = $result->getData('thread_id_2');
    			
    		$threadProId = $this->getThreadProId(array($result->getData('thread_id_1')),1);
    		$threadProPics = $this->getThreadProPics($threadProId);
    		    		
    		$data['thread_id_1']['image']="";
			if(array_key_exists($result->getData('thread_id_1'), $threadProPics)){
				$data['thread_id_1']['image']=self::BBS_ATTACTHMENT_FRONT.$threadProPics[$result->getData('thread_id_1')]['path'];
			}
    		$threadProId = $this->getThreadProId(array($result->getData('thread_id_2')),1);
    		$threadProPics = $this->getThreadProPics($threadProId);
    		$data['thread_id_2']['image']="";
    		if(array_key_exists($result->getData('thread_id_2'), $threadProPics)){
    			$data['thread_id_2']['image']=self::BBS_ATTACTHMENT_FRONT.$threadProPics[$result->getData('thread_id_2')]['path'];
    		}
    			
    		$data['thread_id_1']['voteNum'] = $this->getThreadVoteNum($result->getData('thread_id_1'));
    		$data['thread_id_2']['voteNum'] = $this->getThreadVoteNum($result->getData('thread_id_2'));
    			
    		$data['thread_id_1']['replies'] = $result->getData('leftReplies');
    		$data['thread_id_2']['replies'] = $result->getData('rightReplies');
    		
    		$data['thread_id_1']['link'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/'.'thread-'.$data['thread_id_1']['id'].'-1-1.html';
    		$data['thread_id_2']['link'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/'.'thread-'.$data['thread_id_2']['id'].'-1-1.html';
    	}
    	return $data;
    }
	
	/**
	 * 获取频道下的最新博主资讯
	 * @param : Int $cateId 分类ID
	 */
	public function getBlogArticleInfoByCateId($cateId,$delBlogIds,$limit){
	
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$blogArr = $blodIdArr = $uidArr = $uidFollowArr = $blogPicArr = array();
		$sql = 'select DISTINCT(uid) from hb_home_blog WHERE
						uid IN (
							SELECT
								DISTINCT(cat.uid)
							FROM
								hb_zj_blogs_cate_user as cat
							WHERE
								cat.`STATUS` = 1
							AND cat.cate_id IN (%s)
						)
				AND `STATUS` = %s';
		if(!empty($delBlogIds)){
			$sql .= ' AND blogid NOT IN ('.implode(',',$delBlogIds).')';
		}
		$sql .=	' ORDER BY dateline desc limit 0,%s';
		$result = $this->query($sql,array($cateId,0,$limit));
		while($result->fetch_assoc()){
			$sql = 'select `subject`,username,blogid,uid from `hb_home_blog` where uid=%s and status=0 order by dateline desc limit 1';
			$blog = $this->query($sql,array($result->getData('uid')))->fetch_assoc();
			$blogArr[$blog['blogid']] = $blog;
			array_push($blodIdArr,$blog['blogid']);
			array_push($uidArr,$blog['uid']);
		}

		$finalArr = array();
		if($uidArr){
			$sql = 'select uid,following from hb_common_member_count where uid in ('.implode(',',$uidArr).')';
			$uidResult = $this->query($sql);
			while($uidResult->fetch_assoc()){
				$uidFollowArr[$uidResult->getData('uid')] = $uidResult->getData('following');
			}
	
			$sql = 'select filename,blogid from hb_zj_blogs_pic where blogid in ('.implode(',',$blodIdArr).') and disrel=1';
			$picResult = $this->query($sql);
			while($picResult->fetch_assoc()){
				$blogPicArr[$picResult->getData('blogid')] = $picResult->getData('filename');
			}
	
			foreach($blogArr as $key=>$blog){
				if(isset($uidFollowArr[$blog['uid']])){
					$blog['filename'] = '';
					$blog['following'] = $uidFollowArr[$blog['uid']];
					$blog['bloggerid'] = $this->getBloggerIdByUid($blog['uid']);
					if(isset($blogPicArr[$key])) $blog['filename'] = $blogPicArr[$key];
					
					array_push($finalArr,$blog);
				}
			}
		}
		return $finalArr;
	}
	
	/**
	 * 根据Uid获取最新博主咨询id
	 */
	public function getBlogIdByUid($uids){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$blogIdArr = array();
		if(empty($uids)){
			$sql  = 'select Max(blogid) as blogid from hb_home_blog where status=0 GROUP BY uid ORDER BY blogid DESC LIMIT 4';
		}else{
			$sql  = 'select Max(blogid) as blogid from hb_home_blog where uid in ('.implode(',',$uids).') and status=0 GROUP BY uid';
		}
		
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$blogIdArr[] = $result->getData('blogid');
		}
		return $blogIdArr;
	}
	
	/**
	 * 根据博主资讯ID获取博主信息
	 */
	public function getBlogListByBlogId($blogIds){
		
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$blogArr = $blodIdArr = $uidArr = $uidFollowArr = $blogPicArr = array();
		$sql = 'select b.`subject`,b.username,b.blogid,b.uid,c.cate_id from hb_home_blog as b left join hb_zj_blogs_cate_user as c on b.uid=c.uid  where b.status=0 and  blogid in ('.implode(',',$blogIds).')';
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			
			$blogArr[$result->getData('blogid')]['subject'] = $result->getData('subject');
			$blogArr[$result->getData('blogid')]['username'] = $result->getData('username');
			$blogArr[$result->getData('blogid')]['uid'] = $result->getData('uid');
			$blogArr[$result->getData('blogid')]['cateid'] = $result->getData('cate_id');
			array_push($blodIdArr,$result->getData('blogid'));
			array_push($uidArr,$result->getData('uid'));
		}
		
		$finalArr = array();
		if($uidArr){
			$sql = 'select uid,following from hb_common_member_count where uid in ('.implode(',',$uidArr).')';
			$uidResult = $this->query($sql);
			while($uidResult->fetch_assoc()){
				$uidFollowArr[$uidResult->getData('uid')] = $uidResult->getData('following');
			}
		
			$sql = 'select filename,blogid,disrel from hb_zj_blogs_pic where blogid in ('.implode(',',$blodIdArr).') and disrel in (1,3)';
			$picResult = $this->query($sql);
			while($picResult->fetch_assoc()){
				if(isset($blogPicArr[$picResult->getData('blogid')])){
					if($picResult->getData('disrel') == 3){
						$blogPicArr[$picResult->getData('blogid')] = $picResult->getData('filename');
					}
				}else{
					$blogPicArr[$picResult->getData('blogid')] = $picResult->getData('filename');
				}
			}
			foreach($blogArr as $key=>$blog){
				if(isset($uidFollowArr[$blog['uid']])){
					$blog['filename'] = '';
					$blog['following'] = $uidFollowArr[$blog['uid']];
					$blog['bloggerid'] = $this->getBloggerIdByUid($blog['uid']);
					if(isset($blogPicArr[$key])){
						$blog['filename'] = $blogPicArr[$key];
					} 
					$finalArr[$key] = $blog;
				}
			}
		}
		return $finalArr;
	}
	/**
	 * 根据uid获取博主Id
	 */
	public function getBloggerIdByUid($uid){
		$sql = 'select id from hb_zj_blogs_cate_user where uid = '.$uid;
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			return $result->getData("id");
		}
	}

	/**
	 * 根据fid 获取论坛板块资讯
	 */
	public function getBlogListByFid($fid,$limit = 6){	
		$blogList = array();
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$sql = "SELECT
				 th.`tid` as tid,
				 th.`subject` as blog_subject,
				 po.`subject`,
				 po.message as blog_message
				FROM
				 `hb_forum_thread` AS th
				LEFT JOIN hb_forum_post AS po ON th.tid = po.tid
				WHERE
				 th.displayorder = 0
				AND th.fid = %s
				AND po.invisible = 0
				AND po.position = 1
				ORDER BY
				 th.dateline DESC
				LIMIT 0,%s";
		$result = $this->query($sql,array($fid,$limit));
		while ($result->fetch_assoc()){
			$temp = array();
			$temp['tid'] = $result->getData('tid');
			$temp['blog_subject'] = $result->getData('blog_subject');
			$temp['blog_message'] = substr($result->getData('blog_message'),0,20);
			array_push($blogList,$temp);
		}
		return $blogList;
		
	}
	
	
	/**
	 * 公用的获取帖子的方法
	 * @param array $param -  dateline:在线时间多少个小时, groupId:版块id,type:排序类型1为最热 2为最近更新 3最新创建,limit:最多显示几条,width:导读图或封面图的宽,height:导读图或封面图的高
	 * @return Ambigous <multitype:, NULL, \DateTime>
	 */
	public function getThreadData($param){
		$data = array();
		$sqlWhere = '';
		$groupId = '' ;
		$threadProId = array();
		$threadProPics = array();
		$tids = array();
		$i = 0;
		$width = $param['width'];
		$height = $param['height'];
		
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$sqlWhere .= ' and a.fid<> 18485 ';
		if($param['dateline']){
			$time = time()-3600*$param['dateline'];
			$sqlWhere .= ' and a.dateline >= '.$time.' ';
		}
		if(isset($param['groupId']) && $param['groupId']){
			$sqlWhere .= " and a.fid in (".$param['groupId'].") ";
		}
		if($param['type'] == '3') {
			$orderby = 'a.tid';
		} elseif($param['type'] == '2') {
			$orderby = 'a.lastpost';
		} else {
			$orderby = 'a.views';
		}
		$start = 0;
		$limit = $param['limit'];
		$sql="SELECT
				a.subject,
				a.author,
				a.views,
				a.replies,
				a.heats,
				a.tid,
				a.fid,
				a.authorid
			FROM
				hb_forum_thread AS a
			WHERE a.displayorder >= 0
			".$sqlWhere."
			ORDER BY ".$orderby." DESC
					LIMIT $start, $limit";
		
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$data[$i]['subject'] = $result->getData('subject');
			$data[$i]['author'] = $result->getData('author');
			$data[$i]['views'] = $result->getData('views');
			$data[$i]['replies'] = $result->getData('replies');
			$data[$i]['heats'] = $result->getData('heats');
			$data[$i]['tid'] = $result->getData('tid');
			$data[$i]['fid'] = $result->getData('fid');
			$data[$i]['authorid'] = $result->getData('authorid');
			$data[$i]['attachmentPath'] = '';
			$data[$i]['link'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/thread-'.$result->getData('tid').'-1-1.html';
			$tids[] = $result->getData('tid');
			$i++;
		}
		$threadProId = $this->getThreadProId($tids,1);
		$threadProPics = $this->getThreadProPics($threadProId,$width,$height);
		
		foreach($data as $key => $value){
			if(isset($threadProPics[$value['tid']]['path']) && !empty($threadProPics[$value['tid']]['path'])){
				$data[$key]['attachmentPath'] = self::BBS_ATTACTHMENT_FRONT.$threadProPics[$value['tid']]['path'];
			}else{
				$data[$key]['attachmentPath'] = self::BBS_USER_PORTRAIT.$value["authorid"].'&size=big';
			}
		}		
		return $data;
	}
	
	/**
	 * 
	 * @param int/array $tids 帖子的id 
	 * @param int $proType 1:导读图,2:封面图   (3,4,5,6为导读图的几个尺寸)
	 * @return Array $data 导读图的id
	 */
	public function getThreadProId($tid,$proType=1){
		
		if(!$tid) return array();
		$tid = (array)$tid;
		$proPicId = array();
		$proPicIdKey = array();
		$proPicDiff = array();
		
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$str = implode(',',$tid);
		$sql="select tid,aid from hb_zj_thread_manual_pic_byx where tid in ($str) and type=$proType";
		
		$proPic=$this->query($sql);
		while($proPic->fetch_assoc()){
			$proPicId[$proPic->getData('tid')] = $proPic->getData('aid');
		}
		$proPicIdKey = array_keys($proPicId);
		if(is_array($tid)){
			$proPicDiff = array_diff($tid, $proPicIdKey);
		}else{
			if(!$proPicId[$tid]){
				$proPicDiff = $tid;
			}
		}
		
		if($proPicDiff)
		{	$tid = join(',', $proPicDiff); 		
			//取附件第一张
			$sql="select tid,aid from hb_forum_attachment where tid in (".$tid.") group by tid order by pid,aid asc ";
			$proPic=$this->query($sql);
			while($proPic->fetch_assoc()){
				$proPicId[$proPic->getData('tid')] = $proPic->getData('aid');
			}
		
		}		
		return $proPicId;
	}
	
	/**
	 * 取导读图片
	 * @param Array $proPicId
	 * @return array:$data
	 */
	public function getThreadProPics($proPicId){
		if(empty($proPicId)) return array();
		$attachmentId = 0;
		$table = '';
		$attachment = 0;
		$aid = 0;
		$data = array();
		
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		foreach($proPicId as $key => $value){
			$sql="select aid,tableid from hb_forum_attachment where aid=$value";
			$attachmentId = $this->query($sql);
			while($attachmentId->fetch_assoc()){
				$attachment = $attachmentId->getData('tableid');
				$aid = $attachmentId->getData('aid');
			}
			if($attachment || $attachment==0){
				$table = 'hb_forum_attachment_'.$attachment;
				$sql = "select attachment from $table where aid = $aid ";
				$result = $this->query($sql);
				while($result->fetch_assoc()){						
					$data[$key]['path'] = $result->getData('attachment');
				}
			}
		}	
		return $data;	  
	}
	
	/**
	 * @param : Array $queryCondition 查询条件
	 */
	public function getChannelArticleToBrandOrStar($queryCondition){
		$sqlTable = $queryCondition['tableName'];
		$subSql = $dateSql = '';
		if(!empty($queryCondition['Status'])){
			$subSql .= ' where Status='.$queryCondition['Status'];
		}
		if($sqlTable == 'Brand'){
			if($subSql){
				$subSql .= ' and TagId <> 955';
			} else {
				$subSql .= ' where TagId <> 955';
			}
		}
		$sql ='SELECT
				a.ArticleId as ArticleId,
				b.TagId as TagId,
		        b.ParentId as ParentId,
				c.Title as Title,
				c.ShortTitle as ShortTitle,
		        c.PublishId as PublishId
			FROM
				ArticleProperty a
			LEFT JOIN Article c ON a.ArticleId = c.Id
			LEFT JOIN ArticleTag b ON a.ArticleId = b.ArticleId
			WHERE
				b.TagId IN (SELECT tagId FROM '.$sqlTable.$subSql.')
		    AND c.PublishTime <=%s AND c.Published=1 AND c.Level>=3
			ORDER BY 
				c.PublishTime DESC
			LIMIT 0,
			 '.$queryCondition["limit"].'';
		$result = $this->query($sql,array($queryCondition['startDate']));
		$queryArr = array();
		while ($result->fetch_assoc()){
			
			$temp = array();
			$temp['TagId'] = $result->getData('TagId');
			$temp['ParentId'] = $result->getData('ParentId');
			$temp['ArticleId'] = $result->getData('ArticleId');
			$temp['Title'] = $result->getData('Title');
			$temp['ShortTitle'] = $result->getData('ShortTitle');
			$temp['PublishId'] = $result->getData('PublishId');
			$queryArr[$result->getData('ArticleId')] = $temp;
		}
		return $queryArr;
	}

	
	public function getRelDataByTagIds($tagIds,$type){
		$queryArr = array();
		if($type == 1){
			$sql = 'select b.TagId,b.ImgId,b.Id,b.NameCN,b.NameEN,b.CurDesigner,br.FansNum from Brand b left join BrandStatisticalInfo br on b.Id=br.BrandId where b.Status=1 and TagId in ('.implode(',',$tagIds).')';
		}else{
			$sql = 'select c.TagId,c.Id,c.NameCn,c.NameEn,cr.FansCount,c.Birthday,ci.XingZuo from Celebrity c left join CelebrityRiseCount cr on c.Id=cr.CelebrityId left join CelebrityInfo ci on c.Id=ci.CelebrityId where TagId in ('.implode(',',$tagIds).')';
		}
		$result = $this->query($sql);
		$data = new \haibao\cms\data\Tag();
		$tagModels = $data->getTagsById($tagIds); 
		
		while ($result->fetch_assoc()){
			
			$temp = array();
			if($type == 1){
				$temp['Id'] = $result->getData('Id');
				$temp['NameCn'] = $result->getData('NameCN');
				$temp['NameEn'] = $result->getData('NameEN');
				$temp['CurDesigner'] = $result->getData('CurDesigner');
				$temp['FansCount'] = $result->getData('FansNum');				
				$temp['ImgId'] = $result->getData('ImgId');
			}else{
				$temp['Id'] = $result->getData('Id');
				$temp['NameCn'] = $result->getData('NameCn');
				$temp['NameEn'] = $result->getData('NameEn');
				$temp['Birthday'] = $result->getData('Birthday');
				$temp['FansCount'] = $result->getData('FansCount');
				$temp['XingZuo'] = $result->getData('XingZuo');
				$temp['ImgId'] = 0;
			}
			$temp['Url'] = "";
			foreach ($tagModels as $k=>$tag){
				if($tag["Id"] == $result->getData('TagId')){
					$temp['Url'] = $tag["Url"];
					break;
				}
			}
			
			$queryArr[$result->getData('TagId')] = $temp;
		}
		return $queryArr;
	}
	
	/**
	 * 根据标签ID获取文章信息 需要排除指定的文章
	 */
	public function getArticlesByTagIds($tagIds,$articleIds = ''){
		
		$queryArr = array();
		//$sql = 'select art.Id,art.Title,art.ShortTitle,tag.TagId,art.PublishId from `Article` as art left join `ArticleTag` as tag on art.Id=tag.ArticleId where art.Id not in ('.implode(',',$articleIds).') and art.Published = %s and tag.TagId in ('.implode(',',$tagIds).') group by tag.TagId order by art.PublishTime desc limit 2';
		$sql = 'select art.Id,art.Title,art.ShortTitle,tag.TagId,tag.ParentId,art.PublishId from `Article` as art left join `ArticleTag` as tag on art.Id=tag.ArticleId where art.Id not in ('.$articleIds.') and art.Published = %s and tag.TagId in ('.$tagIds.') order by art.PublishTime desc limit 1';
		$relResult = $this->query($sql,array(1));
		$articleId = $tagId = 0;
		while ($relResult->fetch_assoc()){
			$temp = array();
			$queryArr[$relResult->getData('TagId')]['Title'] = $relResult->getData('Title');
			$queryArr[$relResult->getData('TagId')]['ArticleId'] = $relResult->getData('Id');
			$queryArr[$relResult->getData('TagId')]['PublishId'] = $relResult->getData('PublishId');
			$queryArr[$relResult->getData('TagId')]['TagId'] = $relResult->getData('TagId');
			$queryArr[$relResult->getData('TagId')]['ShortTitle'] = $relResult->getData('ShortTitle');
			$articleId = $relResult->getData('Id');
			$tagId = $relResult->getData('TagId');
		}
		return array($queryArr,$articleId,$tagId);
	}
	
	public function queryFilter($filter,$isGetOne = true){
		if($isGetOne){
			$result = $this->getOne($filter);	
		}else{
			$result = $this->getAll($filter);
		}
		return $result;
	}
	
	/**
	 * 根据明星库中分类为模特、风格偶像、时尚编辑、时尚博主、设计师，查询最新有资讯更新明星的资讯Id
	 */
    public function getArticleIdByStarType($limit=3,$isLike=1){
        
        $sql = "SELECT
        	a.ArticleId AS Aid,
        	a.TagId AS Tid
        FROM
        	`ArticleTag` AS a
        LEFT JOIN Celebrity AS c ON a.TagId = c.TagId LEFT JOIN Article as Art ON a.ArticleId = Art.Id
        WHERE
        	a.ParentId = 1383 and Art.Published=1 and Art.Level > 3
        AND ";
        if($isLike){
        	$sql .="(
        	c.StarTypes LIKE '%[3]%'
        	OR c.StarTypes LIKE '%[7]%'
        	OR c.StarTypes LIKE '%[11]%'
        	OR c.StarTypes LIKE '%[6]%'
        	OR c.StarTypes LIKE '%[15]%'
        	)";
        }else{
        	$sql .="(
        	c.StarTypes NOT LIKE '%[3]%'
        	OR c.StarTypes NOT LIKE '%[7]%'
        	OR c.StarTypes NOT LIKE '%[11]%'
        	OR c.StarTypes NOT LIKE '%[6]%'
        	OR c.StarTypes NOT LIKE '%[15]%'
        	)";
        }
        $sql .=" AND c.`Status` = 1 ORDER BY
        	a.ArticleId DESC
        LIMIT 200";
        
        $relResult = $this->query($sql);
        $arrAIds = array();
        while ($relResult->fetch_assoc()){
            $tid = $relResult->getData('Tid');
            if(!isset($arrAIds[$tid])){
                $arrAIds[$tid] = $relResult->getData('Aid');
            }
        }
        return $arrAIds;
    }
    
    public function getLowerChannelById($pid){
	    $sql = 'select * from SiteChannel where ParentId=%s and Status=%s order by OrderNo asc';
	    $relResult = $this->query($sql,array($pid,1));
	    $array = array();
	    $arrChannel = array();
	    while ($relResult->fetch_assoc()){
	        $array['ChannelName'] = $relResult->getData('ChannelName');
	        $array['ChannelUrl'] = $relResult->getData('ChannelUrl');
	        array_push($arrChannel,$array);
	    }
	    return $arrChannel;
	} 
	
	public function getLowerChannelByParentName($pname){
	    $sql = 'SELECT
                	*
                FROM
                	SiteChannel
                WHERE
                	ParentId = (
                		SELECT
                			Id
                		FROM
                			SiteChannel
                		WHERE
                			ParentId = %s
                		AND STATUS = %s
                		AND ChannelName = %s
                	)
                AND STATUS = %s
                ORDER BY
                    OrderNo ASC';
	    $relResult = $this->query($sql,array(0,1,$pname,1));
	    $array = array();
	    $arrChannel = array();
	    while ($relResult->fetch_assoc()){
	        $array['ChannelName'] = $relResult->getData('ChannelName');
	        $array['ChannelUrl'] = $relResult->getData('ChannelUrl');
	        array_push($arrChannel,$array);
	    }
	    return $arrChannel;
	}
    
    /**
     * 得到投票数
     * @param unknown $tid
     * @return Ambigous <number, NULL, \DateTime>
     */
    public function getThreadVoteNum($tid){
    	$data = 0;
    	$sql="select votecount from hb_zj_pk_vote_byx where thread_id = %s ";
    	$result = $this->query($sql,array($tid));
    	while($result->fetch_assoc()){
    		$data = $result->getData('votecount');
    	}
    	return $data;
    }
    
    /**
     * BBS站点版块
     */
    public function getWebSitePlate(){
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
    	$today = strtotime(date('Y-m-d'));
    	$tidArray = array();
    	$result = null;
    	$data = array();
    	$webSitePlate = $this->bbsPlate();      
    	$webSitePlateKey = array_keys($webSitePlate);
    	if($webSitePlateKey){
    		$keyStr=join(',',$webSitePlateKey);
    	}else{
    		$keyStr='12';
    	}    	
    	$yeastaday = $today-86400;
    	
    	$sql = "select count(tid) as threadNum,fid from hb_forum_thread where fid IN ($keyStr) and dateline>='".$today."' group by fid ";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data[$result->getData('fid')]['fid'] = $result->getData('fid');
    		$data[$result->getData('fid')]['threadNum'] = $result->getData('threadNum');    		
    		$data[$result->getData('fid')]['sitePlate'] = $webSitePlate[$result->getData('fid')];
    		$data[$result->getData('fid')]['flink'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/forum-'.$result->getData('fid').'-1.html';
    	}
    	$result=null;
    	$sql = "SELECT fid,count(pid) as repliesNum From hb_forum_post where fid IN ($keyStr) and dateline>='".$today."' and first = 0 group by fid";
    	$result = $this->query($sql);  	
    
    	while($result->fetch_assoc()){
    		$data[$result->getData('fid')]['repliesNum'] = $result->getData('repliesNum');    	
    	}
 
    	foreach($webSitePlate as $key => $value){
    		if(!isset($data[$key]['fid'])){
    			$data[$key]['fid'] = $key;
    		}
    		if(!isset($data[$key]['threadNum'])){
    			$data[$key]['threadNum'] = 0;
    		}
    		if(!isset($data[$key]['repliesNum'])){
    			$data[$key]['repliesNum'] = 0;
    		}
    		if(!isset($data[$key]['sitePlate'])){
    			$data[$key]['sitePlate'] = $webSitePlate[$key];
    		}
    		if(!isset($data[$key]['flink'])){
    			$data[$key]['flink'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/forum-'.$key;
    		}
    	}    
    	$result=null;
    	$sql="SELECT				
					max(a.tid) as tid,
					a.fid
				FROM
					`hb_forum_thread` AS a
				LEFT JOIN hb_forum_post AS b ON a.tid = b.tid				
				WHERE
					a.fid IN ($keyStr)
				AND a.heats >= 0
				AND replies > 0
				AND a.displayorder >= 0
				and b.position = 1
				and b.invisible = 0
				GROUP BY a.fid ";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$tidArray[] = $result->getData('tid');
    		$data[$result->getData('fid')]['tid'] = $result->getData('tid');
    		$data[$result->getData('fid')]['link'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/thread-'.$result->getData('tid').'-1-1.html';
    	}
    
    	$threadProId = array();
    	$threadProPics = array();
    	if($tidArray){
    		$threadProId = $this->getThreadProId($tidArray,1);
    		$threadProPics = $this->getThreadProPics($threadProId);
    	}
    	
    	$result = Null;
    	
    	$sql = "select thread.authorid,thread.subject,thread.tid,thread.fid,post.from_wx from hb_forum_thread as thread left join hb_forum_post as post on thread.tid = post.tid where thread.tid in (".join(',',$tidArray).")";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data[$result->getData('fid')]['subject'] = $result->getData('subject');
    		$data[$result->getData('fid')]['fromWx'] = $result->getData('from_wx');
    		
    		if(array_key_exists($result->getData('tid'), $threadProPics)){
    			$data[$result->getData('fid')]['hasProPic'] = 1;
    			$data[$result->getData('fid')]['image'] = $threadProPics[$result->getData('tid')]['path'];
    		}else{
    			$data[$result->getData('fid')]['hasProPic'] = 0;
    			$data[$result->getData('fid')]['image'] = self::BBS_USER_PORTRAIT.$result->getData("authorid").'&size=big';
    		}
    	} 
    
    	foreach($webSitePlate as $key => $value){
    		$ccc[$key] = $data[$key];
    	}     	  
    	return $ccc;
    }
    
    
    public function getNewProductAndBrand($param){
    	$result = array();
    	$data = array();
    	$i = 0;    	
    	 $sql="SELECT
    			ProModel.Id,						
				ProModel.ProductName,
				ProModel.PicId,
				Bra.TagId as BrandTagId,
				Bra.Id as BrandId,
    	    	Bra.NameCN as NameCN,
                Bra.NameEN as NameEN,
    	        CategoryId,
    	        CategoryName
				FROM
					Product AS Pro
				INNER JOIN ProductBrandGroup AS ProBG ON Pro.Id = ProBG.ProductId
				INNER JOIN Brand as Bra ON ProBG.BrandId = Bra.Id
				INNER JOIN ProductModel as ProModel on Pro.Id = ProModel.ProductId
    	        INNER JOIN ProductCategoryDefine as ProCate on Pro.CategoryId = ProCate.Id
				WHERE Pro.STATUS = 1 AND Bra.ImgId>0 and ProModel.PicId>0 and ProModel.Status=1 and Bra.Status = 1 and ProCate.status = 1
				GROUP BY ProBG.BrandId ORDER BY Pro.CreateTime desc limit 0, ".$param['limit']."";
    
    	$result = $this->query($sql);    	
    	while($result->fetch_assoc()){
    		$data[$i]['Id'] = $result->getData('Id');
    		$data[$i]['BrandId'] = $result->getData('BrandId');
    		$data[$i]['ProductName'] = $result->getData('ProductName');
    		$data[$i]['PicId'] = $result->getData('PicId');
    		$data[$i]['BrandTagId'] = $result->getData('BrandTagId');
    		$data[$i]['NameCN'] = $result->getData('NameCN');
    		$data[$i]['NameEN'] = $result->getData('NameEN');
    		$data[$i]['CategoryId'] = $result->getData('CategoryId');
    		$data[$i]['CategoryName'] = $result->getData('CategoryName');
    		$i++;
    	}
    	/*$sql = "SELECT
            	Goods.Id,
            	Goods.Name,
              Gimg.ImageId,
            	Bra.TagId AS BrandTagId,
            	Bra.Id AS BrandId,
            	Bra.NameCN AS NameCN,
            	Bra.NameEN AS NameEN,
              Prela.CategoryId,
            	CategoryName
            FROM
            	Product_New AS Pro
            INNER JOIN ProductBrandGroup_New AS ProBG ON Pro.Id = ProBG.ProductId
            INNER JOIN Brand AS Bra ON ProBG.BrandId = Bra.Id
            INNER JOIN Goods AS Goods ON Pro.Id = Goods.ProductId
            INNER JOIN GoodsImage as Gimg on Goods.Id = Gimg.GoodsId
            INNER JOIN ProductCategoryRelation as Prela on Prela.ProductId = Pro.Id
            INNER JOIN ProductCategoryDefine_New AS ProCate ON Prela.CategoryId = ProCate.Id
            WHERE
            	Pro. STATUS = 1
            AND Bra.ImgId > 0
            AND Gimg.ImageId > 0
            AND Goods. STATUS = 1
            AND Bra. STATUS = 1
            AND ProCate. STATUS = 1
            GROUP BY
            	ProBG.BrandId
            ORDER BY
            	Pro.CreateTime DESC
            LIMIT 0, ".$param['limit']."";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    	    $data[$i]['Id'] = $result->getData('Id');
    	    $data[$i]['BrandId'] = $result->getData('BrandId');
    	    $data[$i]['ProductName'] = $result->getData('Name');
    	    $data[$i]['PicId'] = $result->getData('ImageId');
    	    $data[$i]['BrandTagId'] = $result->getData('BrandTagId');
    	    $data[$i]['NameCN'] = $result->getData('NameCN');
    	    $data[$i]['NameEN'] = $result->getData('NameEN');
    	    $data[$i]['CategoryId'] = $result->getData('CategoryId');
    	    $data[$i]['CategoryName'] = $result->getData('CategoryName');
    	    $i++;
    	}*/
    	return $data;    	    	
    }
    
    
    public function getNewProductAndBrandList($param){
    	$result = array();
    	$data = array();
    	$i = 0;
   
    	$sql = "SELECT
    	 Goods.Id,
    	Goods.Name,
    	Gimg.ImageId,
    	Bra.TagId AS BrandTagId,
    	Bra.Id AS BrandId,
    	Bra.NameCN AS NameCN,
    	Bra.NameEN AS NameEN,
    	Prela.CategoryId,
    	CategoryName,
    	ProCateMap.ParentId
    	FROM
    	Product_New AS Pro
    	INNER JOIN ProductBrandGroup_New AS ProBG ON Pro.Id = ProBG.ProductId
    	INNER JOIN Brand AS Bra ON ProBG.BrandId = Bra.Id
    	INNER JOIN Goods AS Goods ON Pro.Id = Goods.ProductId
    	INNER JOIN GoodsImage as Gimg on Goods.Id = Gimg.GoodsId
    	INNER JOIN ProductCategoryRelation as Prela on Prela.ProductId = Pro.Id
    	INNER JOIN ProductCategoryDefine_New AS ProCate ON Prela.CategoryId = ProCate.Id
    	INNER JOIN ProductCategoryMap_New AS ProCateMap 
    	WHERE
    	Pro. STATUS = 1
    	AND Bra.ImgId > 0
    	AND Gimg.ImageId > 0
    	AND Goods. STATUS = 1
    	AND Bra. STATUS = 1
    	AND ProCate. STATUS = 1
    	AND ProCateMap.CategoryId = Prela.CategoryId
    	GROUP BY
    	ProBG.BrandId
    	ORDER BY
    	Pro.CreateTime DESC
    	LIMIT 0, ".$param['limit']."";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    	$data[$i]['Id'] = $result->getData('Id');
    	$data[$i]['BrandId'] = $result->getData('BrandId');
    	$data[$i]['ProductName'] = $result->getData('Name');
    	$data[$i]['PicId'] = $result->getData('ImageId');
    	$data[$i]['BrandTagId'] = $result->getData('BrandTagId');
    	$data[$i]['NameCN'] = $result->getData('NameCN');
    	$data[$i]['NameEN'] = $result->getData('NameEN');
    	$data[$i]['CategoryId'] = $result->getData('CategoryId');
    	$data[$i]['CategoryName'] = $result->getData('CategoryName');
    	$data[$i]['ParentId'] = $result->getData('ParentId');
    	$i++;
    	}
    	return $data;
    }
    
    /**
     * 得到同时含有明星和品牌的文章
     * @param array $param
     * @return Ambigous <multitype:, NULL, \DateTime>
     */
    public function getStarExample($param){
    	$result = array();
    	$data = array();
    	$i = 0;
    	/*$sql="SELECT DISTINCT Art.Id,ArtTag.TagId,ArtTag.ArticleId,Art.Title,ArtPro.ClickNum,ArtPro.CommentNum,Art.PublishTime
		FROM
			ArticleTag ArtTag
		INNER JOIN Article as Art ON Art.Id = ArtTag.ArticleId
		INNER JOIN ArticleTag ArtTag1 ON ArtTag.ArticleId = ArtTag1.ArticleId
		INNER JOIN Brand Bra ON Bra.TagId = ArtTag.TagId
		INNER JOIN Celebrity Cel ON Cel.TagId = ArtTag1.TagId
		Left JOIN	ArticleProperty ArtPro on Art.Id = ArtPro.ArticleId
		WHERE Art.`Level`>0 and Art.Published=1 AND Art.Status=0
		ORDER BY
			ArtTag.ArticleId DESC
		LIMIT 6";*/
    	$sql="SELECT
				Art.Id,
				Art.Title,
    			Art.PublishTime,
    	        Art.PublishId
			FROM
				Article Art		
			WHERE
				Art.Id IN (
					SELECT
						a1.ArticleId
					FROM
						ArticleTag a1
					INNER JOIN Brand b ON b.TagId = a1.TagId
					WHERE
						a1.ArticleId IN (
							SELECT
								a2.ArticleId
							FROM
								ArticleTag a2
							INNER JOIN Celebrity c ON c.TagId = a2.TagId where c.Status=1 and c.TagId <> 955
						) and b.Status=1 and b.TagId <> 955
				) and Art.`Level`>0 and Art.Published=1 AND Art.Status=0
			ORDER BY
				Art.Id DESC
			LIMIT 0, 6";
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data[$i]['Id'] = $result->getData('Id');
    		$data[$i]['Title'] = $result->getData('Title');
    		$data[$i]['PublishId'] = $result->getData('PublishId');    		
    		$data[$i]['PublishTime'] = $result->getData('PublishTime');    		
    		$i++;
    	}
    	return $data;
    }
    
    public function getStarByArticle($articleId){
    	if(empty($articleId)) return array();
    	$result = array();
    	$data = array();    	
    	$articleIdString = '';
    	$articleIdString = join(',',$articleId);
    	$sql="SELECT
					ArT.TagId,
					ArT.ArticleId,
				    ArT.ParentId,
					Cel.ImageId,
					Img.Filename,
    				Bra.Id,
    				Bra.NameCn,
    				Bra.NameEn,
    				CelRC.EditFansNum as FansCount
				FROM
					ArticleTag AS ArT
				INNER JOIN Celebrity AS Bra 
				ON ArT.TagId = Bra.TagId
				LEFT JOIN CelebrityImage as Cel
				on Bra.TagId = Cel.TagId
				left JOIN ImageLibrary as Img 
				on Cel.ImageId = Img.Id
    			left join CelebrityRiseCount as CelRC
    			on Bra.Id = CelRC.CelebrityId
				WHERE
				ArT.TagId<>1383 and Cel.ImageType = 1 and Bra.Status = 1 AND ArT.ArticleId IN (".$articleIdString.") group by ArT.ArticleId";
    	
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data[$result->getData('ArticleId')]['TagId'] = $result->getData('TagId');
    		$data[$result->getData('ArticleId')]['ArticleId'] = $result->getData('ArticleId');
    		$data[$result->getData('ArticleId')]['Filename'] = $result->getData('Filename');
    		$data[$result->getData('ArticleId')]['NameCn'] = $result->getData('NameCn');
    		$data[$result->getData('ArticleId')]['NameEn'] = $result->getData('NameEn');
    		$data[$result->getData('ArticleId')]['StarId'] = $result->getData('Id');
    		$data[$result->getData('ArticleId')]['FansCount'] = $result->getData('FansCount');
    	}
    	return $data;
    }
    
    public function getBrandByArticle($articleId){
    	if(empty($articleId)) return array();
    	$result = array();
    	$data = array();
    	$articleIdString = '';
    	$articleIdString = join(',',$articleId);
    	$sql="SELECT
					ArT.TagId,
					ArT.ArticleId,
				    ArT.ParentId,
					Bra.ImgId,
    				Bra.Id,
					Img.Filename,
    				Bra.NameCN,
    				Bra.NameEN,  
    			  	BraSI.FansNum			
				FROM
					ArticleTag AS ArT
				INNER JOIN Brand AS Bra
				ON ArT.TagId = Bra.TagId				
				left JOIN ImageLibrary as Img
				on Bra.ImgId = Img.Id
    			left join BrandStatisticalInfo as BraSI
    			on Bra.Id = BraSI.BrandId
				WHERE
				 ArT.TagId<>955 and Bra.Status = 1 AND ArT.ArticleId IN (".$articleIdString.") group by ArT.ArticleId";
    	 
    	$result = $this->query($sql);
    	while($result->fetch_assoc()){
    		$data[$result->getData('ArticleId')]['TagId'] = $result->getData('TagId');
    		$data[$result->getData('ArticleId')]['ArticleId'] = $result->getData('ArticleId');
    		$data[$result->getData('ArticleId')]['Filename'] = $result->getData('Filename');
    		$data[$result->getData('ArticleId')]['NameCn'] = $result->getData('NameCN');
    		$data[$result->getData('ArticleId')]['NameEn'] = $result->getData('NameEN');
    		$data[$result->getData('ArticleId')]['BrandId'] = $result->getData('Id');
    		$data[$result->getData('ArticleId')]['FansCount'] = $result->getData('FansNum');
    	}
    	return $data;
    }
    
   /**
    * 得到指定的活动频道页中的文章版块
    * @return multitype:|Ambigous <multitype:, NULL, \DateTime>
    */
    public function getArticlePlate(){
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
    	$result = array();
    	$data = array();
    	$articleIds = array();
    	$i = 0;
    	$sql="SELECT
					max(Art.Id) as id,
					ArtTag.ParentId,
					count(Art.Id) as articleNum
				FROM
					Article AS Art
				LEFT JOIN ArticleTag AS ArtTag ON (Art.Id = ArtTag.ArticleId)
				WHERE
					Art. STATUS = 0
				AND Art.Published = 1
				AND Art.PublishTime >= '2015-03-10 00:00:00'
				AND ArtTag.IsPrimaiy = 1
				AND ArtTag.ParentId IN (
					310,
					24,
					955,
					6863,
					7455,
					1383,
					520,
					211
				)
				GROUP BY ArtTag.ParentId";
    	$result = $this->query($sql);
    	
    	while($result->fetch_assoc()){
    		$data[$result->getData('id')]['tid'] = $result->getData('id');
    		$data[$result->getData('id')]['fid'] = $result->getData('ParentId');
    		$data[$result->getData('id')]['threadNum'] = $result->getData('articleNum');
    		$articleIds[] = $result->getData('id');    		
    	}
    	
    	$result = array();
    	if($articleIds){
    		$sql="SELECT
					Art.Id AS id,
					Art.Title,
					ArtPer.CommentNum,
    		        Art.PublishId
				FROM
					Article AS Art
				LEFT JOIN ArticleProperty AS ArtPer ON Art.Id = ArtPer.ArticleId 
				WHERE	Art.Id IN (".join(',',$articleIds).")";
    		$result = $this->query($sql);
    		while($result->fetch_assoc()){
    			$data[$result->getData('id')]['subject'] = $result->getData('Title');
    			$data[$result->getData('id')]['repliesNum'] = $result->getData('CommentNum');
    			$data[$result->getData('id')]['PublishId'] = $result->getData('PublishId');
    		}
    	}else{
    		return array();
    	}    	
    	return $data;
    }
    
	/**
	 * 根据标签ID获取资讯的主标签
	 * 该板块下属资讯流量最高的前5篇的主标签
	 * @param : Array $tagId
	 * @param : Int $limit
	 */
    public function getArticlePrimaryTagByTagId($tagId,$limit = 5,$orderBy = 1){
    	
    	// DO : 获取点击量高的文章
    	$sql = 'SELECT
					`at`.TagId as TagId,
					`at`.ArticleId as ArticleId,
					ap.ClickNum as ClickNum
  
				FROM
					ArticleTag AS `at`
				LEFT JOIN ArticleProperty AS ap ON `at`.ArticleId = ap.ArticleId
				WHERE';
    	if(is_array($tagId)){
    		$sql .= '`at`.TagId IN ('.implode(',',$tagId).')
    		ORDER BY ap.ClickNum desc limit %s
    		;';
    	}else{
    		$sql .= '`at`.TagId = '.$tagId.'
    		ORDER BY ap.ClickNum desc limit %s
    		;';
    	}
				
    	$result = $this->query($sql,array($limit));
    	$articleIdArr = $primaryIdArr = array();
    	while($result->fetch_assoc()){
    		array_push($articleIdArr,$result->getData('ArticleId'));
    	}
    	
    	if($articleIdArr){
    	// DO : 获取文章主标签
    		$sql = 'select TagId from ArticleTag where IsPrimaiy = 1 and ArticleId in ('.implode(',',$articleIdArr).')';
    		$result = $this->query($sql);
    		while ($result->fetch_assoc()){
    			array_push($primaryIdArr,$result->getData('TagId'));
    		}
    	}
    	return $primaryIdArr;
    }
    /**
     * 获取博主最新推荐
     * @param : $limit 条数
     * @param : $notin 限制的条数
     */
    public function getBloggerRecommand($limit = 4,$notin=array()){
    	
    	$this->initBlogDB();
    	$notinSql = '';
    	if($notin){
    		$ids = implode(',',$notin);
    		$notinSql = " and id not in(".$ids.") "; 
    	}
    	$sql = 'select id,cate_id,uid,uname from `hb_zj_blogs_cate_user` where status =%s and tuijian=%s '.$notinSql.' order by addtime desc limit '.$limit.'';
    	$result = $this->query($sql,array(1,1));
    	$bloggers = array();
    	while($result->fetch_assoc()){
    		if(isset($bloggers[$result->getData('uid')])){
    			$bloggers[$result->getData('uid')]['uid'] = $result->getData('uid');
    			$bloggers[$result->getData('uid')]['cate_id'] = $result->getData('cate_id');
    			$bloggers[$result->getData('uid')]['id'] = $result->getData('id');
    			$bloggers[$result->getData('uid')]['uname'] = $result->getData('uname');
    		}else{
    			$bloggers[$result->getData('uid')] = array();
    			$bloggers[$result->getData('uid')]['uid'] = $result->getData('uid');
    			$bloggers[$result->getData('uid')]['cate_id'] = $result->getData('cate_id');
    			$bloggers[$result->getData('uid')]['id'] = $result->getData('id');
    			$bloggers[$result->getData('uid')]['uname'] = $result->getData('uname');
    		}
    	}
    	return $bloggers;
    }
    
    /**
     * 根据Ids取得博主推荐
     * @param : $ids 博主id
     */
    public function getBloggerRecommandByIds($ids){
    	$this->initBlogDB();
    	if($ids){
    		$uids = implode(',',$ids);
    	}else{
    		return array();
    	}
    	$sql = 'select id,cate_id,uid,uname from `hb_zj_blogs_cate_user` where status =%s and tuijian=%s and uid in ('.$uids.') order by addtime desc';
    	$result = $this->query($sql,array(1,1));
    	$bloggers = array();
    	while($result->fetch_assoc()){
    		if(isset($bloggers[$result->getData('uid')])){
    			$bloggers[$result->getData('uid')]['uid'] = $result->getData('uid');
    			$bloggers[$result->getData('uid')]['cate_id'] = $result->getData('cate_id');
    			$bloggers[$result->getData('uid')]['id'] = $result->getData('id');
    			$bloggers[$result->getData('uid')]['uname'] = $result->getData('uname');
    		}else{
    			$bloggers[$result->getData('uid')] = array();
    			$bloggers[$result->getData('uid')]['uid'] = $result->getData('uid');
    			$bloggers[$result->getData('uid')]['cate_id'] = $result->getData('cate_id');
    			$bloggers[$result->getData('uid')]['id'] = $result->getData('id');
    			$bloggers[$result->getData('uid')]['uname'] = $result->getData('uname');
    		}
    	}
    	return $bloggers;
    }
    
    /**
     * 根据博主ID获取博主最新发布的资讯
     * @param : Array $uid 博主ID
     */
    public function getBlogsByUid($uid){

    	$limit = count($uid);
    	$uid = implode(',',$uid);
    	$this->initBlogDB();
    	$sql = 'SELECT
  				  b.blogid,
				  b.`subject`,
				  b.uid,
				  p.filename
				FROM
					hb_home_blog as b left join hb_zj_blogs_pic as p on b.blogid=p.blogid 
				WHERE
					b.uid IN ('.$uid.') and b.stauts=0 group by b.uid 
				ORDER BY
					b.dateline desc limit '.$limit.'';
    	$result = $this->query($sql);
    	$blogList = array();
    	while($result->fetch_assoc()){
    		if(isset($blogList[$result->getData('uid')])){
    			$blogList[$result->getData('uid')]['subject'] = $result->getData('subject');
    			$blogList[$result->getData('uid')]['filename'] = $result->getData('filename');
    			$blogList[$result->getData('uid')]['blogid'] = $result->getData('blogid');
    		}else{
    			$blogList[$result->getData('uid')] = array();
    			$blogList[$result->getData('uid')]['subject'] = $result->getData('subject');
    			$blogList[$result->getData('uid')]['filename'] = $result->getData('filename');
    			$blogList[$result->getData('uid')]['blogid'] = $result->getData('blogid');
    		}
    	}
    	return $blogList;
    }

    /**
     * 获取博主最新更新资讯
     */
    public function getNewBlogList($currentPage,$limit = 6){

    	$this->initBlogDB();
    	$sql = 'SELECT
					b.blogid,
					b.`subject`,
					b.uid,
					b.dateline,
					u.id,
					u.username,
					c.cate_id
				FROM
					hb_home_blog AS b
				INNER JOIN auth_user AS u ON b.uid = u.id
				LEFT JOIN hb_zj_blogs_cate_user as c on b.uid = c.uid
    			WHERE b.`status` = 0 and b.`picflag` = 1 and c.`status` = 1
				ORDER BY
					b.dateline DESC
				LIMIT '.($currentPage - 1)*$limit.','.$limit.'';
    	$result = $this->query($sql);
    	$blogArr = array();
    	while($result->fetch_assoc()){
    		$temp = array();
    		$temp['blogid'] =  $result->getData('blogid');
    		$temp['subject'] =  $result->getData('subject');
    		$temp['uid'] =  $result->getData('uid');
    		if($result->getData('cate_id')){
    			$temp['cate_id'] =  $result->getData('cate_id');
    		}else{
    			$temp['cate_id'] =  93;
    		}
    		$temp['id'] =  $result->getData('id');
    		$temp['uname'] =  $result->getData('username');
    		$temp['dateline'] =  $result->getData('dateline');
    		$blogArr[$result->getData('blogid')] = $temp;
    	}
    	if($blogArr){
    		
    		$blogIds = array_keys($blogArr);
    		$blogPicArr = $this->getBlogPic($blogIds);
    		foreach($blogArr as $key=>$arr){
    			if(isset($blogPicArr[$key])){
    				$blogArr[$key]['filename'] = $blogPicArr[$key];
    			}else{
    				$blogArr[$key]['filename'] = '';
    			}
    		}
    	}
    	return $blogArr;
    }
    
    /**
     * 根据资讯ID获取博文导读图
     * @param : $blogIds
     */
    public function getBlogPic($blogIds){
    	
    	$sql = 'select filename,blogid from hb_zj_blogs_pic where disrel = 1 and blogid in ('.implode(',',$blogIds).')';
    	$result = $this->query($sql);
    	
    	$blogPicArr = array();
    	while($result->fetch_assoc()){
    		$blogPicArr[$result->getData('blogid')] = $result->getData('filename');
    	}
    	return $blogPicArr;
    }
    

    /**
     * 获取时间范围内点击量最高的博主资讯
     */
    public function getBlogByClick($dateType,$limit = 10){

    	$this->initBlogDB();
    	if($dateType == 1){
    		
    		$startDate = strtotime(date('Y-m-d H:i:s',strtotime('-1 weeks')));
    		$endDate = time();
    	}
    
    	$sql = 'SELECT
    			    b.dateline,
					b.`subject`,
				  	b.blogid,
				  	b.viewnum_false
				FROM
					hb_home_blog AS b where b.dateline >=%s and b.dateline <%s and b.status=0
				ORDER BY
						b.viewnum_false DESC
				LIMIT '.$limit.'';
    	$result = $this->query($sql,array($startDate,$endDate));
    	$blogArr = array();
    	while($result->fetch_assoc()){
    		$temp = array();
    		$temp['blogid'] =  $result->getData('blogid');
    		$temp['subject'] =  $result->getData('subject');
    		$temp['viewnum_false'] =  $result->getData('viewnum_false');
    		$temp['dateline'] =  date('Y-m-d H:i:s',$result->getData('dateline'));
    		$blogArr[$result->getData('blogid')] = $temp;
    	}
    	if($blogArr){
    	
    		$blogIds = array_keys($blogArr);
    		$blogPicArr = $this->getBlogPic($blogIds);
    		foreach($blogArr as $key=>$arr){
    			if(isset($blogPicArr[$key])){
    				$blogArr[$key]['filename'] = $blogPicArr[$key];
    			}else{
    				$blogArr[$key]['filename'] = '';
    			}
    		}
    	}
    	return $blogArr;
    	
    }
    
    private function initNewDB() {
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
    	return $this;
    }
    private function initOldDB() {
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE );
    	return $this;
    }

    public function initBlogDB() {
    	$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
    	return $this;
    }
    
    private function bbsPlate(){
    	$data = array();
    	$cacheName = self::$webSitePlateCatche;
    	$cacheTagArr = $this->getCache($cacheName);   
    	$cacheTagArr = array();
    	if(empty($cacheTagArr)){
    		$this->initBlogDB();
    		$sql="select fid,name from hb_forum_forum where status = 1 and displayorder>100 order by displayorder asc";
    		$result = $this->query($sql);
    		while($result->fetch_assoc()){
    			$cacheTagArr[$result->getData('fid')] = $result->getData('name');
    		}
    		$this->setCache($cacheName, serialize($cacheTagArr));
    	}else{
    		return unserialize($cacheTagArr);
    	}
    	return $cacheTagArr;
    }
}